# _*_ coding utf-8 _*_
# george
# time: 2025/1/11上午11:56
# name: database.py
# comment:
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session, declarative_base

from app.config.config import config
from app.settings import env


def db_connection():
    # 创建引擎,连接数据库
    engine = create_engine(config[env].db_url, echo=config[env].if_echo)
    # 打开数据库的连接会话
    session = sessionmaker(engine)
    # 保证线程安全
    db_session = scoped_session(session)
    # 获取基类,类似于我们创建的Model
    Base = declarative_base()
    return db_session, Base,engine

